package com.xq.shop.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.xq.shop.po.User;
import com.xq.shop.service.user.IUserService;
import com.xq.shop.util.Const;
import com.xq.shop.util.UserServiceFactory;

@Controller
@RequestMapping("/user/")
public class LoginCtrl {

	private IUserService userService = UserServiceFactory.getUserService();
	
	/**
	 * 用户登录
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping("user_login.do")
	public ModelAndView login(String username, String password, HttpServletRequest request, HttpServletResponse response){
		
		User user = userService.get(username);	//获取用户信息
		
		/*在session中设置常用信息*/
		HttpSession session =  request.getSession();
		if(user != null && StringUtils.isNotBlank(password) &&
				password.equals(user.getPassword())){
			
			session.setAttribute(Const.User_Session, user);
			return new ModelAndView("user/index").addObject("user", user);
		} else {
			return new ModelAndView("home");
		}
	}
	
	@RequestMapping("user_logout.do")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response){
		
		request.getSession().removeAttribute(Const.User_Session);
		return new ModelAndView("home");
	}
	
//	public ModelAndView adminIndex(HttpServletResponse response){
//		
//	}
}
